/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.common.model.DistributionMsg;
import com.yami.shop.distribution.common.service.DistributionMsgService;
import com.yami.shop.security.platform.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionMsg")
@Api(tags = "平台端分销通知接口")
public class DistributionMsgController {

    @Autowired
    private DistributionMsgService distributionMsgService;

    @GetMapping("/page")
    @ApiOperation(value = "分页获取分销通知列表")
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:page')")
    public ServerResponseEntity<IPage<DistributionMsg>> page(DistributionMsg distributionMsg, PageParam page) {
        IPage<DistributionMsg> list = distributionMsgService.getDistributionMsgsAndSysUserPage(page, distributionMsg);
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:info')")
    @ApiOperation(value = "根据分销通知id查看分销通知信息")
    @ApiImplicitParam(name = "id", value = "分销通知id", required = true, dataType = "Long")
    public ServerResponseEntity<DistributionMsg> info(@PathVariable("id") Long id) {
        DistributionMsg distributionMsg = distributionMsgService.getById(id);
        return ServerResponseEntity.success(distributionMsg);
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:save')")
    @ApiOperation(value = "保存分销通知信息")
    public ServerResponseEntity<Void> save(@RequestBody @Valid DistributionMsg distributionMsg) {
        distributionMsg.setUpdateTime(new Date());
        distributionMsg.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionMsg.setLevel(1);
        distributionMsg.setShopId(Constant.PLATFORM_SHOP_ID);
        distributionMsgService.save(distributionMsg);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:update')")
    @ApiOperation(value = "修改分销通知信息")
    public ServerResponseEntity<Void> update(@RequestBody @Valid DistributionMsg distributionMsg) {
        distributionMsg.setUpdateTime(new Date());
        distributionMsg.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionMsgService.updateById(distributionMsg);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:delete')")
    @ApiOperation(value = "根据分销通知id列表批量删除分销通知信息")
    @ApiImplicitParam(name = "ids", value = "分销通知id", required = true, dataType = "Long")
    public ServerResponseEntity<Void> delete(@RequestBody List<Long> ids) {
        distributionMsgService.removeByIds(ids);
        return ServerResponseEntity.success();
    }
}
